/**
 * LevelFile.java
 * 18/05/2011 12:31:05
 */
package controller;

import gui.dialogs.EndLevelDialog;
import gui.menus.MenuLevelActions;

import java.beans.PropertyChangeListener;
import java.net.URL;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.PropertySupport;
import resources.GlobalSettings;
import resources.UrlBundle;

/**
 * @author walber
 * 
 */
public class LevelFileController implements MenuLevelActions, ChangeListener {

	private PropertySupport<URL> levelFile;
	private PropertySupport<Integer> currentLevel;

	public LevelFileController() {
		this.currentLevel = new PropertySupport<Integer>(
				GlobalSettings.defaultLevel);
		this.levelFile = new PropertySupport<URL>(
				UrlBundle.get(GlobalSettings.defaultLevelName
						+ this.currentLevel.get()));
	}

	private void createFile() {
		levelFile.set(UrlBundle.get(GlobalSettings.defaultLevelName
				+ this.currentLevel.get()));
	}

	/**
	 * @see gui.menus.MenuLevelActions#actionNextLevel()
	 */
	@Override
	public void actionNextLevel() {
		if (canNextLevel()) {
			this.currentLevel.set(this.currentLevel.get() + 1);
			createFile();
		}
	}

	/**
	 * @see gui.menus.MenuLevelActions#actionBackLevel()
	 */
	@Override
	public void actionBackLevel() {
		if (canBackLevel()) {
			this.currentLevel.set(this.currentLevel.get() - 1);
			createFile();
		}
	}

	/**
	 * @see gui.menus.MenuLevelActions#actionRestartLevel()
	 */
	@Override
	public void actionRestartLevel() {
		this.levelFile.setChanged(null);
	}

	/**
	 * @see gui.menus.MenuLevelActions#canNextLevel()
	 */
	@Override
	public boolean canNextLevel() {
		return this.currentLevel.get() < GlobalSettings.maximumLevel;
	}

	/**
	 * @see gui.menus.MenuLevelActions#canBackLevel()
	 */
	@Override
	public boolean canBackLevel() {
		return this.currentLevel.get() > GlobalSettings.minimumLevel;
	}

	/**
	 * @see gui.menus.MenuLevelActions#addCurrentLevelPropertyChangeListener(PropertyChangeListener)
	 */
	@Override
	public void addCurrentLevelPropertyChangeListener(
			PropertyChangeListener listener) {
		this.currentLevel.addPropertyChangeListener(listener);
	}

	/**
	 * @see gui.menus.MenuLevelActions#removeCurrentLevelPropertyChangeListener(PropertyChangeListener)
	 */
	@Override
	public void removeCurrentLevelPropertyChangeListener(
			PropertyChangeListener listener) {
		this.currentLevel.removePropertyChangeListener(listener);
	}

	/**
	 * Agregar un listener a la lista de file level.
	 * 
	 * @param listener
	 */
	public void addLevelFilePropertyChangeListener(
			PropertyChangeListener listener) {
		this.levelFile.addPropertyChangeListener(listener);
	}

	/**
	 * Borrar un listener de la lista de file level.
	 * 
	 * @param listener
	 */
	public void removeLevelFilePropertyChangeListener(
			PropertyChangeListener listener) {
		this.levelFile.removePropertyChangeListener(listener);
	}

	/**
	 * Tratar evento que informa que se ha llegado al final del nivel actual.
	 * 
	 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
	 */
	@Override
	public void stateChanged(ChangeEvent e) {
		EndLevelDialog.showDialog();
		if (canNextLevel() == false) {
			// Ya no hay mas niveles de dificultad, fin.
			System.exit(0);
		}
		actionNextLevel();
	}
}
